package com.example.bai6;


import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	EditText edtbd;
    EditText edtcd;
    EditText edtnm;
    CheckBox chkgender;
    ListView list;
    AutoCompleteTextView autotext;
    Button btnadd;
    Button btnbrowse;
    MyAdapter adapter;
    ArrayList<Student> arr;
    
    ArrayAdapter<String> autoadapter;
    ArrayList<String> autoarr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWidget();
        addEventForWidget();
        fakeData();
    }
    
    private void getWidget()
    {
    	edtbd= (EditText) findViewById(R.id.edtbd);
        edtcd= (EditText) findViewById(R.id.edtcd);
        edtnm= (EditText) findViewById(R.id.edtnm);
        chkgender= (CheckBox) findViewById(R.id.chkgender);
        btnadd= (Button) findViewById(R.id.btnadd);
        btnbrowse= (Button) findViewById(R.id.btnbrowse);
        
        list= (ListView) findViewById(R.id.list);
        arr= new ArrayList<Student>();
        adapter= new MyAdapter(this, R.layout.list_layout, arr);
        list.setAdapter(adapter);
        
        autotext= (AutoCompleteTextView) findViewById(R.id.autotext);
        autoarr= new ArrayList<String>();
        autoadapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,autoarr);
        autotext.setAdapter(autoadapter);
    }
    
    private void addEventForWidget()
    {
    	btnadd.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				proccessInput();
			}
		});
    	
    	btnbrowse.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				proccessBirthday();
			}
		});
    }
    
    private void proccessBirthday()
    {
    	DatePickerDialog dpd= new DatePickerDialog(MainActivity.this,new OnDateSetListener() {
			
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				edtbd.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear)+"/"+String.valueOf(year));
			}
		},
    	1990,1,1);
    	
    	dpd.setTitle("Birthday");
    	dpd.show();
    }
    
   private void proccessInput()
   {
	   String cd= edtcd.getText().toString();
	   String nm=edtnm.getText().toString();
	   String bd= edtbd.getText().toString();
	   String bp=autotext.getText().toString();
	   boolean gr= chkgender.isChecked();
	   SimpleDateFormat sdf= new SimpleDateFormat("dd/mm/yyyy",Locale.getDefault());
	   try
	   {Student s= new Student(cd, nm, gr, sdf.parse(bd), bp);
	   arr.add(s);
	   autoadapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,autoarr);
       autotext.setAdapter(autoadapter);
	  // adapter.notifyDataSetChanged();
	   
	   proccessAuto(bp);
	   }
	   catch(Exception ex)
	   {		
		   Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
	   }
   }
    
   private void proccessAuto(String para)
   {
	   for(int i=0;i<autoarr.size();i++)
	   {
		   if(autoarr.get(i).equalsIgnoreCase(para.trim()))
			   return;
	   }
	   autoarr.add(para.trim());
	   autoadapter.notifyDataSetChanged();
	   Toast.makeText(this, para, Toast.LENGTH_LONG).show();
		
   }
   public void fakeData()
   {
   Student s1=new Student("1", "Đoàn Ái Nương", true, new Date(1980-1900, 2, 2), "TP. Hồ Chí Minh");
   Student s2=new Student("2", "Nguyễn Thùy Trang", true, new Date(1982-1900, 3, 3), "Lâm Đồng");
   Student s3=new Student("3", "Hoàng Văn Phúc", false, new Date(1970-1900, 4, 4), "Hà Nội");
   Student s4=new Student("4", "Đinh Hồng Lợi", false, new Date(1972-1900, 4, 4), "Bắc Giang");
   Student s5=new Student("5", "Nguyễn Hoàng Uyên", true, new Date(1970-1900, 4, 4), "Huê");
   arr.add(s1);
   arr.add(s2);
   arr.add(s3);
   arr.add(s4);
   arr.add(s5);
   adapter.notifyDataSetChanged();
   }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
